package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.CartFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-08-01 17:02
 **/

@Controller
@RequestMapping("wcart")
public class CartController {

    @Autowired
    private CartFeign cartFeign;


    /**
     * @param model
     * @return java.lang.String
     * @description: 从Redis中通过用户名查询购物车数据
     * @author 大佬味的小男孩
     * @date 2020/08/01 17:06
     */
    @GetMapping("list")
    public String List(Model model) {
        Map map = cartFeign.list();
        for (Object key : map.keySet()) {
            System.out.println(key +":"+map.get(key));
        }
        model.addAttribute("items", map);
        return "cart";
    }

    /**
     * @param id
     * @param num
     * @return com.changgou.entity.Result<java.util.Map>
     * @description: 添加商品到购物车
     * @author 大佬味的小男孩
     * @date 2020/08/01 17:06
     */
    @GetMapping("add")
    @ResponseBody
    public Result<Map> add(String id, Integer num) {
        cartFeign.addCart(id, num);
        Map map = cartFeign.list();
        return new Result<Map>(true, StatusCode.OK, "添加购物车成功", map);

    }

}
